<?php
namespace Home\Controller;

class DrinkController extends CommonController{
    
    //饮水列表
    public function index(){
        //饮水文案
        $this->assign('words',M('Words')->field('drink')->find(1));
        
        $p = I('request.p') ? I('request.p') : 1;
        $list = M('Drink')->where(array('uid'=>$this->uid))->order('id desc')->page($p.',10')->select();
        foreach($list as $k=>$v){
            $list[$k]['create_time'] = fmt_date($v['create_time']);
        }
        
        if($p == 1){
            $this->assign('list',$list);
            $this->display();
        }else{//无限加载
            if(empty($list)){
                echo json_encode('err');die;
            }else{
                echo json_encode($list);die;
            }
        }
    }
    
    //记录饮水
    public function edit(){
        $Drink = D('Drink');
        $create_date = date('Y-m-d');
        if(IS_POST){
            //保存饮水
            $res = $Drink->add(array(
                'uid'=>$this->uid,'vol'=>I('post.vol'),'create_date'=>$create_date
            ));
            
            D('Score')->addDrink($this->uid);//积分-增加
            
            $res ? die('suc') : die('err');
        }
    }
    
    public function del(){
        $Drink = M('Drink');
        $id = I('post.id');
        $row = $Drink->find($id);
        if($row['uid'] == $this->uid){
            $res = $Drink->delete($id);
            $res ? die('suc') : die('err');
        }else{
            die('err');
        }
    }
    
    //饮水图表
    public function chart(){
        if($pid = I('request.pid')){
            $this->uid = I('request.pid');
        }else{
            $pid = '';
        }
        $this->assign('pid',$pid);
        
        //饮水文案
        $this->assign('words',M('Words')->field('drink')->find(1));
        
        //饮水图表
        $current_time = time();
        $list = array();$listx = array();$listy = array();
        $create_date = date('Y-m-d',$current_time);
        
        //生成图表纵横轴数据
        if(I('get.type') == 'week'){//本周
            $dateArr = getNowWeekDate();
            $list = M('Drink')->field("*,sum(vol) as sum_vol")
                              ->where(array('uid'=>$this->uid,'create_date'=>array('in',$dateArr)))
                              ->group('create_date')
                              ->order('id asc')
                              ->select();
            foreach($list as $v){
                $listx[] = substr($v['create_date'],5,5);
                $listy[] = $v['sum_vol'];
            }
        }else{//本天
            $list = M('Drink')->field("*,sum(vol) as sum_vol")
                              ->where(array('uid'=>$this->uid,'create_date'=>$create_date))
                              ->group('create_date')
                              ->order('id asc')
                              ->select();
            foreach($list as $v){
                $listx[] = substr($v['create_time'],11,5);
                $listy[] = $v['sum_vol'];
            }
        }

        $this->assign('xStr',"'".implode("','",$listx)."'");
        $this->assign('yStr',implode(",",$listy));
        $this->display();
    }
    
}